from django import forms
from SelectApp import models


class RegisterForm(forms.Form):
    name = forms.CharField(label='昵称',
                           max_length=10,
                           widget=forms.TextInput(attrs={'class': 'lowin-input', 'autocomplete': 'off'}),
                           error_messages={
                               'required': '*必填',
                               'max_length': '昵称最大10位'
                           })
    user = forms.CharField(label='用户名',
                           max_length=20,
                           min_length=6,
                           widget=forms.TextInput(attrs={'class': 'lowin-input', 'autocomplete': 'off'}),
                           error_messages={
                               'required': '*必填',
                               'max_length': '用户名最大20位',
                               'min_length': '用户名最小6位'
                           })
    password = forms.CharField(label='密码',
                               max_length=20,
                               min_length=6,
                               widget=forms.PasswordInput(attrs={'class': 'lowin-input', 'autocomplete': 'off'}),
                               error_messages={
                                   'required': '*必填',
                                   'max_length': '密码最大20位',
                                   'min_length': '密码最小6位'
                               })
    confirm_password = forms.CharField(label='确认密码',
                                       max_length=20,
                                       min_length=6,
                                       widget=forms.PasswordInput(attrs={'class': 'lowin-input', 'autocomplete': 'off'}),
                                       error_messages={
                                           'required': '*必填',
                                       })
    email = forms.EmailField(label='邮箱',
                             widget=forms.TextInput(attrs={'class': 'lowin-input', 'autocomplete': 'off'}),
                             error_messages={
                                 'required': '*必填',
                             })

    def clean_name(self):
        name = self.cleaned_data.get('name')
        u = models.User.objects.filter(name=name).first()
        if u:
            self.add_error('name', '该昵称已被使用')
        else:
            return name

    def clean_user(self):
        user = self.cleaned_data.get('user')
        u = models.User.objects.filter(user=user).first()
        if u:
            self.add_error('user', '该用户名已被注册')
        else:
            return user

    def clean_email(self):
        email = self.cleaned_data.get('email')
        u = models.User.objects.filter(email=email).first()
        if u:
            self.add_error('email', '该邮箱已被注册')
        else:
            return email

    def clean(self):
        password = self.cleaned_data.get('password')
        confirm_password = self.cleaned_data.get('confirm_password')
        if password == confirm_password:
            return self.cleaned_data
        else:
            self.add_error('confirm_password', '两次输入密码不一致')

